<?php
class Application_Model_Filter_WayBillSearch {
    /**
     * 快速搜索
     */
    public $quick;
    /**
     * 渠道公司
     */
    public $agentid;
    /**
     * 发货渠道
     */
    public $expressId;
    /**
     * 开始时间
     */
    public $starttime;
    /**
     * 结束时间
     */
    public $endtime;
    /**
     * 左边状态
     */
    public $status;
    /**
     * 问题
     */
    public $problem;
    /**
     * 用户Id
     */
    public $userid;
    /**
     * 左边未制作发票
     */
    public $invoice;
    /**
     * 今天
     */
    public $day;
    /**
     * 目的地国家ID
     */
    public $cityid;
    /**
     * 开始重量
     */
    public $startweight;
    /**
     * 结束重量
     */
    public $endweight;
    /**
     * 开始预付重量
     */
    public $startpayment;
    /**
     * 结束预付重量
     */
    public $endpayment;
    /**
     * 开始预付款
     */
    public $startmoney;
    /**
     * 结束预付款
     */
    public $endmoney;
    /**
     * 右边未打印地址
     */
    public $isAddform;
    /**
     * 右边未制作发票
     */
    public $isinvoice;
    /**
     * 排序方式
     */
    public $order;
    
    function getSearchFileds() {
        $where = array();
        $and = '';
        $or = '';
        $this->order = ' datePaid desc ';
        $isProblem = 1;
        if ($this->quick != '') {
            $quick = trim($this->quick);
            $num = strlen($quick);
            $where["( "] = 1;
            $or = " and ";
            if ($num == 5) {
                $firstLetter = substr($quick,0,1);
                if(strtoupper($firstLetter) == "F"){$quick = "P".strtoupper(substr($quick,1,5));}
                $user = Application_Model_User::bystore($quick);
                if ($user) {
                    $where[$or . ' userId = '] = $user->id;
                    $or = " or ";
                } else {
                    $where[$or . ' userId = '] = "";
                    $or = " or ";
                }
            } else if ($num == 11) {
                $storeItem = Application_Model_StoreItem::bybarcode($quick);
                $wayid = "(";
                if ($storeItem) {
//                    foreach ($storeItem as $storevalue) {
                    $wayItem = $storeItem->getStoreIdWay();
                    if ($wayItem) {
                        foreach ($wayItem as $wayval) {
                            $wayid .= $wayval->wayId . ",";
                        }
                    }
//                    }
                }
                $wayid .= "0)";
                $where[$or . " id in "] = $wayid;
                $or = " or ";
                $where[$or . ' agentNo = '] = $quick;
            } else {
                $where[$or . ' billNo like '] ='%'. $quick.'%';
                $or = " or ";
                $where[$or . ' agentNo like '] = '%'. $quick.'%';
                $where[$or . ' logistics = '] = $quick;
            }
            $where[") and "] = "1=1";
            $and = ' and ';
            $isProblem = 0;
        }else{
            if($this->day == ""){
                if ($this->starttime != '') {
                    $where[$and  . 'datePaid >='] = $this->starttime . " 00:00:00";
                    $and = ' and ';
                }
                if ($this->endtime != '') {
                    $where[$and  . 'datePaid <='] = $this->endtime . " 23:59:59";
                    $and = ' and ';
                }
            }
        }
        if ($this->agentid != '') {
            $where[$and . ' agentId = '] = $this->agentid;
            $and = ' and ';
        }
        if ($this->expressId != '') {
            $where[$and . ' expressId = '] = $this->expressId;
            $and = ' and ';
        }
        if (!is_null($this->status) && $this->status != "") {
            if(is_array($this->status)){
                  $statusVal = $this->status;
                  $where[$and . " status > "] =  $statusVal["0"];
            }else{
            $where[$and . " interior = "] = $this->status;
            $and = " and ";
            }
            if ($this->status == Application_Model_wayBillItem::Interior_Processing || $this->status >= Application_Model_wayBillItem::Interior_Outstorage) {
                $this->order = ' datePaid asc ';
            }
        }else{
//            $where[$and . " interior != "] = Application_Model_wayBillItem::Interior_Launch;
//            $and = " and ";
        }
//        var_dump($this->problem);
        
        
        if ($this->userid != null) {
            $where[$and . ' userId = '] = $this->userid;
            $and = ' and ';
            $isProblem = 0;
        }
        if ($this->problem == Application_Model_wayBillItem::Problem_Appear) {
            $where[$and . " problem = "] = $this->problem;
            $and = " and ";
            $where[$and . " interior != "] = Application_Model_wayBillItem::Interior_Cancel;
        } else {
            if ($isProblem == 1) {
                $where[$and . " ( problem = " . Application_Model_wayBillItem::Problem_default . " or problem is null or problem = " . Application_Model_wayBillItem::Problem_Aolve . ") and "] = 1;
                $and = " and ";
            }
        }
        if ($this->invoice != 0) {
            $where[$and . ' (isInvoice = 0 or isInvoice is null) and '] = 1;
            $and = ' and ';
            $where[$and . " interior < "] = Application_Model_wayBillItem::Interior_Outstorage;
            $where[$and . " interior > "] = Application_Model_wayBillItem::Interior_WaitPay;
        }
        if ($this->isinvoice != 0) {
            $where[$and . ' (isInvoice = 0 or isInvoice is null) and '] = 1;
            $and = ' and ';
            $where[$and . " interior < "] = Application_Model_wayBillItem::Interior_Outstorage;
            $where[$and . " interior > "] = Application_Model_wayBillItem::Interior_WaitPay;
        }
        if ($this->isAddform != 0) {
            $where[$and . ' isAddform = '] = 0;
            $and = ' and ';
        }
        if ($this->day != null) {
            $where[$and . ' dateCreated > '] = date("Y-m-d");
            $and = ' and ';
        }
        if ($this->cityid != null) {
            $where[$and . " state = "] = $this->cityid;
            $and = " and ";
        }
        if ($this->startweight != null && $this->endweight != null) {
            $where[$and . " productWeight >= "] = $this->startweight * 1000;
            $and = " and ";
            $where[$and . " productWeight <= "] = $this->endweight * 1000;
        }
        if ($this->startpayment != null && $this->endpayment != null) {
            $where[$and . " paymentWeight >= "] = $this->startpayment * 1000;
            $and = " and ";
            $where[$and . " paymentWeight <= "] = $this->endpayment * 1000;
        }
        if ($this->startmoney != null && $this->endmoney != null) {
            $where[$and . " shipMoney >= "] = $this->startmoney;
            $and = " and ";
            $where[$and . " shipMoney <= "] = $this->endmoney;
        }
        return $where;
    }
    
    function getOrderFileds()
    {
        if($this->order != null){
            return $this->order;
        }
        return ' datePaid desc ';
    }
}
